package com.koron.common.core.util;

import com.koron.bean.util.RedisUtils;
import com.koron.common.core.business.configsettings.bean.ConfigDetail;
import com.koron.common.core.business.configsettings.bean.ConfigType;
import com.koron.common.core.business.configsettings.service.ConfigDetailService;
import com.koron.common.core.business.configsettings.service.ConfigTypeService;
import com.koron.common.core.business.datadict.bean.po.SysDataDict;
import com.koron.common.core.business.datadict.bean.po.SysDataDictDetail;
import com.koron.common.core.business.datadict.service.SysDataDictClassifyService;
import com.koron.common.core.business.datadict.service.SysDataDictDetailService;
import org.koron.ebs.mybatis.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

@Component
public class UpdateRedis {

    @Autowired
    private SysDataDictClassifyService sysDataDictClassifyService;

    @Autowired
    private SysDataDictDetailService sysDataDictDetailService;

    @Autowired
    private ConfigTypeService configTypeService;

    @Autowired
    private ConfigDetailService configDetailService;

    private static final String key_prefix_configSettings = "eam:configSettings:";

    private static final String key_prefix_dataDict = "eam:dataDict:";


    public void setConfigTypeInRedis(ConfigType configType) {
        String classifyCode = configType.getClassifyCode();
        String code = configType.getCode();

        // 1. 定义redisKey
        String redisKey = key_prefix_configSettings + classifyCode + ":" + code;
        // 2. 构造redisValue
        Map<String,Object> redisValue = new HashMap<>();
        redisValue.put("id",configType.getId());
        redisValue.put("classifyCode",classifyCode);
        redisValue.put("code",code);
        redisValue.put("name",configType.getName());
        redisValue.put("configDetailList",configType.getConfigDetailList());
        // 3. 更新redis
        RedisUtils.hmset(redisKey,redisValue);
    }

    public void setConfigDetailInRedis(SessionFactory factory,ConfigDetail configDetail) {
        // 1. 获取redisKey
        String redisKey = configDetailService.getRedisKeyByConfigDetail(factory,configDetail);
        // 2. 构造redisValue
        Map<String,Object> redisValue = new HashMap<>();
        redisValue.put("id",configDetail.getId());
        redisValue.put("configId",configDetail.getConfigId());
        redisValue.put("appid",configDetail.getAppid());
        redisValue.put("key",configDetail.getKey());
        redisValue.put("value",configDetail.getValue());
        redisValue.put("description",configDetail.getDescription());
        // 3. 新增/更新redis数据
        RedisUtils.hmset(redisKey,redisValue);
    }

    public void setDataDictInRedis(SysDataDict sysDataDict) {
        String classifyId = sysDataDict.getClassifyId();
        String code = sysDataDict.getCode();

        // 1. 获取redisKey
        String redisKey = key_prefix_dataDict + classifyId + ":" + code;
        // 2. 构造redisValue
        Map<String,Object> redisValue = new HashMap<>();
        redisValue.put("id",sysDataDict.getId());
        redisValue.put("classifyId",classifyId);
        redisValue.put("code",code);
        redisValue.put("name",sysDataDict.getName());
        redisValue.put("sysDataDictDetailList",sysDataDict.getSysDataDictDetailList());
        // 3. 新增/更新redis数据
        RedisUtils.hmset(redisKey,redisValue);
    }

    public void setDataDictDetailInRedis(SessionFactory factory, SysDataDictDetail sysDataDictDetail) {
        // 1. 获取redisKey
        String redisKey = sysDataDictDetailService.getRedisKeyByDataDictDetail(factory,sysDataDictDetail);
        // 2. 构造redisValue
        Map<String,Object> redisValue = new HashMap<>();
        redisValue.put("id",sysDataDictDetail.getId());
        redisValue.put("dictionary",sysDataDictDetail.getDictionary());
        redisValue.put("appid",sysDataDictDetail.getAppid());
        redisValue.put("key",sysDataDictDetail.getKey());
        redisValue.put("value",sysDataDictDetail.getValue());
        redisValue.put("sort",sysDataDictDetail.getSort());
        // 3. 新增/更新 redis
        RedisUtils.hmset(redisKey,redisValue);
    }
}
